package utils;

import java.security.Security;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;


public class AESUtil2 {
    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";
    /**
     * 生成key
     */
    private static SecretKeySpec key = new SecretKeySpec(DecriptUtils.MD5("3201A4567B1023C1981D2005E8223FIG").toLowerCase().getBytes(), ALGORITHM);
    /**
     * AES加密 这个是错的
     *
     * @param data
     * @return
     * @throws Exception
     */
//    public static String encryptData(String data) throws Exception {
//        Security.addProvider(new BouncyCastleProvider());
//        // 创建密码器
//        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");
//        // 初始化
//        cipher.init(Cipher.ENCRYPT_MODE, key);
//        Base64.Decoder decoder = Base64.getDecoder();
//        byte[] base64ByteArr = decoder.decode(data);
//        String result = new String(cipher.doFinal(base64ByteArr));
//        System.out.println("解密结果:{}" + result);
//        return result;
//    }

    /**
     * AES解密
     *
     * @param base64Data
     * @return
     * @throws Exception
     */
    public static String decryptData(String base64Data) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");
        cipher.init(Cipher.DECRYPT_MODE, key);
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] base64ByteArr = decoder.decode(base64Data);
        String result = new String(cipher.doFinal(base64ByteArr));
        System.out.println("解密结果:{}" + result);
        return result;
    }



}